<?php

namespace module\manage\validate;

use app\Validate;
use app\model\Dynamic as Model;

/**
 * 题库 验证
 *
 * @author suiyuan
 */
class Dynamic extends Validate
{
    /**
     * 所有验证场景需要验证的字段.
     *
     * @var array<array>
     */
    protected $scene = [
        'save' => ['id', 'title', 'bank_id', 'type', 'parent_id', 'analysis', 'album'],
    ];
    protected $rule =   [
        'type' => 'require|in:1,2,3,4,5', //   题目类型 1 单选 2多选 3 判断 4 综合
        'title' => 'require|checkTitle', // 题目
        'bank_id' => 'require|exists:app\model\DynamicBank,id|integer', // new
        'parent_id' => 'integer', // 题目为综合题时获取
        'analysis' => 'requireIf:type,1|requireIf:type,2|requireIf:type,3|requireIf:type,4', // 解析
        'fraction' => 'integer|max:100', // 分数
        'album' => 'array|checkAlbum',
        'children' => 'array|requireIf:type,5|checkChildren',
        'album' => 'array|min:1|checkAlbum',
    ];
    
    /**
     * 验证字段
     *
     * @var array
     */
    protected $field = [
        'bank_id' => '题库ID', // 新
        'type' => '题型', //  1 单选 2多选 3 判断 4 综合
        'title' => '题目',
        'parent_id' => '父级ID', // 题目为综合题，获取
        'analysis' => '解析',
        'fraction' => '分数',
        'album' => '答案选项',
        'delete_at' => '删除标识位',
        'create_at' => '创建时间',
        'update_at' => '修改时间',
    ];
    // 自定义验证规则
    protected function checkAlbum($value, $rule, $data=[])
    {
        $validate = new DynamicAlbum;
        foreach($value as $row){
            if(!$validate->check($row)){
                return $validate->getError();
            }
        }
        return true;
    }
}
